package com.globo.devsup.client.presenter;

import java.util.List;

import com.globo.devsup.client.view.IView;
import com.globo.devsup.client.vo.BaseVO;
import com.globo.devsup.client.vo.MessageVO;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.widgets.MessageBox;

public abstract class BasePresenter implements Presenter {

    private IView view;
    private Strategy operation = NoneViewStrategy;
    
    public IView pack(){
        setUpViewListeners();
        return view;
    }
    
    public abstract void setUpViewListeners();
    
    public IView getView(){
        return this.view;
    }
    
    public void setView(IView view){
        this.view = view;
    }

    public Strategy getOperation() {
        return operation;
    }

    public void setOperation(Strategy operation) {
        this.operation = operation;
    }
    
    public AsyncCallback getCallback(final String syncType){
    	AsyncCallback result = new AsyncCallback() {
    		IView view = getView();
    		public void onSuccess(Object result) {
				if(result instanceof MessageVO[]){
					MessageVO[] mgs = (MessageVO[]) result;
					StringBuffer sb = new StringBuffer(); 
					for (int i = 0; i < mgs.length; i++)
						sb.append(mgs[i].getMsg());
					MessageBox.alert(view.getTitle(),"Erro : " + sb.toString());
				}else{
					List<BaseVO> vos = (List<BaseVO>) result;
				/*	if(syncType.equalsIgnoreCase("SYNC_MODEL")){
						updateModelFromView(arrayVo);
					}else{*/
					for(int i = 0; i < vos.size(); i++)
						updateViewFromModel(vos.get(i));
				//	}
		        }
				view.unmask();
		    }
			public void onFailure(Throwable caught) {
				MessageBox.alert(view.getTitle(),"Erro: " + caught.getMessage());
				view.unmask();
			}
        };
    	return result;
    }

}
